package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;

/*
    库管类
        1. 核心职责是管理我们的数据仓库       --数组:增删改查
        2. 这里的所有方法都是给业务员使用
*/


public class StudentDao implements BaseStudentDao{

    private static Student[] stuArray = new Student[5];

    static {
        Student stu1 = new Student("heima001", "阿拉松", "20", "1999-10-11");
        Student stu2 = new Student("heima002", "彭小晏", "21", "1998-10-12");

        stuArray[0] = stu1;
        stuArray[1] = stu2;
    }

    //创建Student学生数组,长度为5

    //添加学生
    public boolean addStudent(Student stu) {

        //遍历数组,利用标记位思想,判断是否能添加进去学生对象
        int index = -1;
        //遍历数组
        for (int i = 0; i < stuArray.length; i++) {
            //获取当前索引位置的元素
            Student student = stuArray[i];
            if (student == null) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            return false;
        } else {
            stuArray[index] = stu;
            return true;
        }
    }

    //查询
    public Student[] findAllStudent() {
        return stuArray;
    }

    //删除学生
    public void deleteStudent(String delId) {
        int index = getIndex(delId);

        //将该索引位置的元素用null替换
        stuArray[index] = null;
    }

    //获取指定学号的索引位置
    public int getIndex(String delId) {
        //查找学生id在数组中的索引位置
        int index = -1;
        for (int i = 0; i < stuArray.length; i++) {
            Student student = stuArray[i];
            if (student != null && student.getId().equals(delId)) {
                index = i;
                break;
            }
        }
        return index;
    }

    //修改学生
    public void updateStudent(String updateId, Student stu) {
        //获取指定学号的索引位置
        int index = getIndex(updateId);
        //将stu覆盖指定索引位置
        stuArray[index] = stu;
    }
}
